<?php

/*

End 1 Player Battle (battle_1p_end.php)

*/

$page_title = "End 1 Player Battle";
$rank_check = 1;
include "header.inc.php";

$findBattle = fetch("SELECT * FROM one_player_battles2 WHERE userid = '$userid' AND game = '$game'");
if (!$findBattle[id])
{
	die(header(error("battle_1player.php?game=$game","This fight does not exsist.")));
}

print $openHTML;

$findEnemy = fetch("SELECT * FROM one_players2 WHERE id = '$findBattle[enemy]' AND game = '$game'");
$findYourPet = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[your_pet]' AND game = '$game'");
$findYourSpecies = fetch("SELECT * FROM pets2 WHERE id = '$findYourPet[species]' AND game = '$game'");

if ($findBattle[enemy_hp] <= 0)
{
	$winner = "you";
}

if ($findYourPet[current_hp] <= 0)
{
	$winner = "enemy";
}

if (($findYourPet[current_hp] <= 0) AND ($findBattle[enemy_hp] <= 0))
{
	$winner = "noone";
}

if ($winner == "you")
{
	$multiplier = rand(21,36);
	$exp = $findEnemy[level] * $multiplier;
	$prize = rand(6,14);
	$winnings = $findEnemy[level] * $prize;

	points($game,"$username", $winnings);
	print "<p align=center><img src=images/user_images/opg_$game/pets/pet_$findYourSpecies[id].gif><br><font size=+2>The Winner!</font></p>";

	print "<p>Congratulations, you beat $findEnemy[name]! $findYourPet[name] has been awarded with $exp experience points and you have been awarded with $winnings $pointsVar!</p>";

	$nextLevel = ($findYourPet[level] * 50) * $findYourPet[level];

	mysql_query("UPDATE user_pets2 SET experience=$findYourPet[experience]+$exp WHERE id = 	'$findYourPet[id]' AND game = '$game'");

	if ($findBattle[enemy_hp] > "-15") { $newAttitude = $findYourPet[attitude]+20; }
	if ($findBattle[enemy_hp] > "-10") { $newAttitude = $findYourPet[attitude]+10; }
	if ($findBattle[enemy_hp] > "-5") { $newAttitude = $findYourPet[attitude]+5; }

	if (($findYourPet[experience]+$exp) > $nextLevel)
	{
		$newLevel = $findYourPet[level] + 1;
		print "<p>$findYourPet[name] has gained a level, now it is level $newLevel!</p>";

		$randStrength = rand(0,2);
		$randIntelligence = rand(0,2);
		$randSpeed = rand(0,2);
		$randDefense = rand(0,2);
		$randHP = rand(0,3);

		mysql_query("UPDATE user_pets2 SET level=$findYourPet[level]+1 WHERE id = '$findYourPet[id]' AND game = '$game'");
		mysql_query("UPDATE user_pets2 SET strength=$findYourPet[strength]+$randStrength WHERE id = '$findYourPet[id]' AND game = '$game'");
		mysql_query("UPDATE user_pets2 SET intelligence=$findYourPet[intelligence]+$randIntelligence WHERE id = '$findYourPet[id]' AND game = '$game'");
		mysql_query("UPDATE user_pets2 SET speed=$findYourPet[speed]+$randSpeed WHERE id = '$findYourPet[id]' AND game = '$game'");
		mysql_query("UPDATE user_pets2 SET defense=$findYourPet[defense]+$randDefense WHERE id = '$findYourPet[id]' AND game = '$game'");
		mysql_query("UPDATE user_pets2 SET max_hp=$findYourPet[max_hp]+$randHP WHERE id = '$findYourPet[id]' AND game = '$game'");
		mysql_query("UPDATE user_pets2 SET current_hp=$findYourPet[current_hp]+$randHP WHERE id = '$findYourPet[id]' AND game = '$game'");

		$findYourPet = fetch("SELECT * FROM user_pets2 WHERE id = '$findBattle[your_pet]' AND game = '$game'");
		print "<p>
		&nbsp;&nbsp;&nbsp;<b>Strength:</B> $findYourPet[strength]<Br>
		&nbsp;&nbsp;&nbsp;<b>Intelligence:</B> $findYourPet[intelligence]<Br>
		&nbsp;&nbsp;&nbsp;<b>Speed:</B> $findYourPet[speed]<Br>
		&nbsp;&nbsp;&nbsp;<b>Defense:</B> $findYourPet[defense]<Br>
		&nbsp;&nbsp;&nbsp;<b>Health:</B> $findYourPet[max_hp]
		</p>";
		$newAttitude = $newAttitude + $findYourPet[attitude] + 20;
	}

	mysql_query("UPDATE one_players2 SET loses=$findEnemy[loses]+1 WHERE id = '$findEnemy[id]' AND game = '$game'");
	mysql_query("UPDATE one_players2 SET guys_left=$findEnemy[guys_left]-1 WHERE id = '$findEnemy[id]' AND game = '$game'");
	mysql_query("UPDATE user_pets2 SET one_p_wins=$findYourPet[one_p_wins]+1 WHERE id = '$findYourPet[id]' AND game = '$game'");
}

if ($winner == "enemy")
{
	print "<p align=center><img src=images/user_images/opg_$game/enemies/enemy_$findEnemy[id].gif><br><font size=+2>The Winner!</font></p>";

	print "<p>$findEnemy[name] won the battle! Please try again soon!</p>";
	mysql_query("UPDATE one_players2 SET wins=$findEnemy[wins]+1 WHERE id = '$findEnemy[id]' AND game = '$game'");
	mysql_query("UPDATE user_pets2 SET one_p_loses=$findYourPet[one_p_loses]+1 WHERE id = '$findYourPet[id]' AND game = '$game'");

	if ($findYourPet[current_hp] > "-15") { $newAttitude = $findYourPet[attitude]-60; }
	if ($findYourPet[current_hp] > "-10") { $newAttitude = $findYourPet[attitude]-30; }
	if ($findYourPet[current_hp] > "-5") { $newAttitude = $findYourPet[attitude]-15; }
}

if ($winner == "noone")
{
	print "<p align=center><img src=images/user_images/opg_$game/enemies/enemy_$findEnemy[id].gif><img src=images/user_images/opg_$game/pets/pet_$findYourSpecies[id].gif><br><font size=+2>Double Knockout!</font></p>";

	print "<p>This was a double knock out, no one won this battle! Please try again soon!</p>";
	mysql_query("UPDATE one_players2 SET guys_left=$findEnemy[guys_left]-1 WHERE id = '$findEnemy[id]' AND game = '$game'");

	$newAttitude = $findYourPet[attitude]-5;
}

print "<p><a href=battle_center.php?game=$game>Back to the battle center!</a></p>";

mysql_query("DELETE FROM one_player_battles2 WHERE userid = '$userid' AND game = '$game'");
mysql_query("UPDATE user_pets2 SET attitude=$newAttitude WHERE id = '$findYourPet[id]' AND game = '$game'");

print $closeHTML;
?>